package cn.tedu.egweibo1.controller;

import cn.tedu.egweibo1.mapper.WeiboMapper;
import cn.tedu.egweibo1.pojo.dto.WeiboDTO;
import cn.tedu.egweibo1.pojo.entity.User;
import cn.tedu.egweibo1.pojo.entity.Weibo;
import cn.tedu.egweibo1.pojo.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
    @RequestMapping("insert")
    public int insertWeibo(@RequestBody WeiboDTO weiboDTO, HttpSession session){
        if (session.getAttribute("user")==null){
            return 2;
        }
        Weibo weibo = new Weibo();
        UserVO userVO = (UserVO) session.getAttribute("user");
        weibo.setContent(weiboDTO.getContent());
        weibo.setCreated(new Date());
        weibo.setId(userVO.getId());
        System.out.println((Long)userVO.getId());
        weiboMapper.insertWeibo(weibo);
        return 1;
    }
}
